home *** CD-ROM | disk | FTP | other *** search
- /*
- * This module provides operations for HTTP items, such as splitting URIs, ...
- */
-
- var EXPORTED_SYMBOLS = [ ];
- Components.utils.import("resource://csfiremodules/CsFireCommon.jsm");
-
- CsFire.HttpUtils = new function() {
- this.CROSSDOMAIN_RELAXED = 0;
- this.CROSSDOMAIN_STRICT = 1;
- };
-
- /*
- * Determines whether a request is internal (within the browser) or is an
- * internet request.
- *
- * This function is mostly taken from the "RequestPolicy" extension
- */
- CsFire.HttpUtils.isRequestInternal = function(uri) {
- var result = false;
- if(uri != null) {
- var scheme = uri.scheme;
- if( scheme == "about" || scheme == "chrome"
- || scheme == "data"
- || scheme == "file"
- || scheme == "javascript"
- || scheme == "moz-icon"
- || scheme == "resource"
- || scheme == "view-source"
- || scheme == "wyciwyg" ) {
- result = true;
- }
- }
- else {
- result = true;
- }
-
- return result;
- };
-
- /*
- * Splits a URI in its different parts. This function provides more info than
- * the nsIUri interface.
- */
- CsFire.HttpUtils.splitUri = function(uri) {
- if(this.isRequestInternal(uri)) {
- // Internal requests often have no host or auth info, which causes errors
- return {"fullUri": null,
- "scheme": uri.scheme,
- "auth": null,
- "host": null,
- "port": null,
- "path": null,
- "item": null,
- "params": null};
- }
- else {
- //extract GET parameters from URI
- var path = uri.path;
- var params = null;
- var splittedParams = null;
- var startParams = uri.path.indexOf('?');
- if(startParams == -1 ) {
- // No parameters available, so set the start index to the end of the path
- startParams = uri.path.length;
- }
- else {
- // Params are available, so extract them from the path and reduce the path
- params = uri.path.substring(startParams, uri.path.length);
- path = path.substr(0, startParams);
-
- // Split the GET parameters and extract the keynames
- splittedParams = this.splitGetParameters(params);
- }
-
- //extract the actual item that's being requested (the 'img.png' of 'http://www.google.be/images/img.png')
- var item = null;
- var startItem = path.lastIndexOf('/');
- if(startItem > 0) {
- item = path.substring(startItem + 1, path.length);
- }
- else if(path.length > 0) {
- item = path.substr(1, path.length);
- }
-
- return {"fullUri": uri.prePath + uri.path,
- "scheme": uri.scheme,
- "auth": uri.userPass,
- "host": uri.host,
- "port": uri.port,
- "path": path,
- "item": item,
- "params": splittedParams};
- }
- };
-
- /*
- * This function retrieves the part of the path for which Basic authentication
- * is valid.
- */
- CsFire.HttpUtils.getAuthenticatedPath = function(uri) {
- var splittedUri = this.splitUri(uri);
- var path = splittedUri.path.substring(0, splittedUri.path.length - splittedUri.item.length);
- return uri.prePath + path;;
- }
-
- /*
- * This function splits the string of GET parameters and returns a string
- * containing all the keynames.
- */
- CsFire.HttpUtils.splitGetParameters = function(stringParams) {
- var paramList = [];
- var params = stringParams.split("?")[1].split("&");
- for(var i = 0; i < params.length; i++) {
- paramList.push(params[i].split("=")[0]);
- }
- return paramList;
- };
-
- /*
- * This function extracts the cookie header from the HTTP channel. Using this
- * header, each cookie name is extracted. For this cookie, the properties (expiry
- * date, path, security) are fetched from the cookie database. The function returns
- * a string containing all this info for all cookies of the channel.
- */
- CsFire.HttpUtils.extractCookies = function(httpChannel) {
- var uri = httpChannel.URI;
-
- try {
- var cookieJar = [];
- var cookieCount = 0; //count separately, since cookieJar.length seems to be incorrect
- var cookieHeader = httpChannel.getRequestHeader("cookie");
- var cookies = cookieHeader.split(";");
-
- for(var i = 0; i < cookies.length; i++) {
- var cookieName = cookies[i].split("=")[0].replace(/^\s*|\s*$/g,''); //strip whitespace from cookienames
- cookieJar[cookieName] = 1;
- cookieCount++;
- }
-
- // Extract more info about cookies, such as path, expiry date and security
- var cookieInfo = [];
-
- var cookieMgr = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager2);
- var count = cookieMgr.countCookiesFromHost(uri.host);
-
- var processedCookies = 0;
- for (var e = cookieMgr.enumerator; e.hasMoreElements() && processedCookies < cookieCount;) {
- var cookie = e.getNext().QueryInterface(Components.interfaces.nsICookie);
- if(uri.host.indexOf(cookie.host) != -1 && cookieJar[cookie.name] != null && cookieJar[cookie.name] == 1) {
- processedCookies++;
- cookieInfo.push(new Array(cookie.name, cookie.path, cookie.isSecure, cookie.expires));
- }
- }
-
- return cookieInfo;
- }
- catch(e) {
- return [];
- }
- };
-
- /*
- * This function extracts the domain name and tld from an URI
- */
- CsFire.HttpUtils.getDomainName = function(stringUri) {
- var startDomain = stringUri.lastIndexOf(".", stringUri.lastIndexOf(".") - 1);
- return stringUri.substring(startDomain + 1, stringUri.length);
- }
-
- /*
- * Checks if the request has any referrer info or not. If any kind of info is
- * available, true is returned, false otherwise.
- */
- CsFire.HttpUtils.hasReferrer = function(data) {
- var result = false;
- if( data.referrer_scheme != null ||
- data.referrer_host != null ||
- data.referrer_port != null ||
- data.referrer_uri != null) {
- result = true;
- }
- return result;
- }
-
- /*
- * This function decides whether a request is cross-domain or not. The first
- * argument determines the level of strictness, while the second contains
- * all the data about the request. The function returns true or false.
- */
- CsFire.HttpUtils.isRequestCrossDomain = function(level, data) {
- var crossDomain = true;
- if(!this.hasReferrer(data)) {
- //Requests with no origin are considered crossdomain --> changed in version 2.4
- CsFire.Logger.debug("Crossdomain check: no referrer info available ==> crossdomain");
- crossDomain = true;
- }
- else {
- if(data.referrer_scheme == "moz-nullprincipal") {
- //Weird firefox scheme, almost never appears
- CsFire.Logger.debug("Crossdomain check: even firefox doesn't know (moz-nullprincipal) ==> crossdomain");
- crossDomain = true;
- }
- else {
- if(data.referrer_scheme == null || data.referrer_scheme == "http" || data.referrer_scheme == "https") {
- //Acceptable scheme, so check cross-domain
-
- if(level == this.CROSSDOMAIN_RELAXED) {
-
- //Only check domain names
- if(CsFire.HttpUtils.getDomainName(data.referrer_host) == CsFire.HttpUtils.getDomainName(data.dst_host)) {
- CsFire.Logger.debug("Crossdomain check (relaxed): same domains ==> not crossdomain");
- crossDomain = false;
- }
- else {
- CsFire.Logger.debug("Crossdomain check (relaxed): different domains ==> crossdomain");
- crossDomain = true;
- }
- }
- else if(level == this.CROSSDOMAIN_STRICT) {
- //Check <scheme, host, port>
-
- if(data.referrer_host == data.dst_host) {
- if((data.referrer_scheme == null || data.dst_scheme == null) || data.referrer_scheme == data.dst_scheme) {
- if((data.referrer_port == null || data.referrer_port == -1 || data.dst_port == null || data.dst_port == -1) || data.referrer_port == data.dst_port) {
- CsFire.Logger.debug("Crossdomain check (strict): same <scheme, host, port> ==> not crossdomain");
- crossDomain = false;
- }
- else {
- CsFire.Logger.debug("Crossdomain check (strict): different ports ==> crossdomain");
- crossDomain = true;
- }
- }
- else {
- CsFire.Logger.debug("Crossdomain check (strict): different schemes ==> crossdomain");
- crossDomain = true;
- }
- }
- else {
- CsFire.Logger.debug("Crossdomain check (strict): different hosts ==> crossdomain");
- crossDomain = true;
- }
- }
- else {
- var message = "Unknown cross-domnain strictness level: " + level;
- CsFire.Logger.error(message);
- throw(message);
- }
- }
- else {
- CsFire.Logger.debug("Crossdomain check: probably internal scheme (" + data.referrer_scheme + ") ==> not crossdomain");
- crossDomain = false;
- }
- }
- }
-
- return crossDomain;
- };
-
- /*
- * Converts the numerical value of the cross-domain level to a textual representation.
- */
- /*CsFire.HttpUtils.convertCrossDomainLevel = function(level) {
- switch(level) {
- case this.CROSSDOMAIN_RELAXED: return "relaxed";
- break;
- case this.CROSSDOMAIN_STRICT: return "strict";
- break;
- }
- }*/
-
-
- /*
- * This function converts the numerical content type to a string representation.
- */
- /*CsFire.HttpUtils.convertContentType = function(contType) {
- switch(contType) {
- case Components.interfaces.nsIContentPolicy.TYPE_OTHER :return "other";
- case Components.interfaces.nsIContentPolicy.TYPE_SCRIPT :return "script";
- case Components.interfaces.nsIContentPolicy.TYPE_IMAGE :return "image";
- case Components.interfaces.nsIContentPolicy.TYPE_STYLESHEET :return "stylesheet";
- case Components.interfaces.nsIContentPolicy.TYPE_OBJECT :return "object";
- case Components.interfaces.nsIContentPolicy.TYPE_DOCUMENT :return "document";
- case Components.interfaces.nsIContentPolicy.TYPE_SUBDOCUMENT :return "subdocument";
- case Components.interfaces.nsIContentPolicy.TYPE_OBJECT :return "object";
- case Components.interfaces.nsIContentPolicy.TYPE_REFRESH :return "refresh";
- case Components.interfaces.nsIContentPolicy.TYPE_XBL :return "xbl";
- case Components.interfaces.nsIContentPolicy.TYPE_PING :return "ping";
- case Components.interfaces.nsIContentPolicy.TYPE_XMLHTTPREQUEST :return "xmlhttprequest";
- case Components.interfaces.nsIContentPolicy.TYPE_TYPE_OBJECT_SUBREQUEST :return "object subrequest";
- }
- };*/
-